﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using ThanhTu.GNC.Media.KQXS.DataAccess.BL;
using ThanhTu.GNC.Media.KQXS.DataAccess.DO;
using System.Data;

namespace GNCMedia.KQXS.Service
{
    /// <summary>
    /// Summary description for GNC_Lottery
    /// </summary>
    [WebService(Namespace = "http://lottery.gncmedia.vn/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class GNC_Lottery : System.Web.Services.WebService
    {
        [WebMethod]
        public int ContentReceive(string shortcode, string msisdn, int mtseq, string cmdcode, string msgbody, string moseq)
        {
            MessengerSyntaxBL vMessengerSyntaxBL = new MessengerSyntaxBL();
            ResultLotteryBL vResultLottery = new ResultLotteryBL();
            SenderMessageBL vSenderMessageBL = new SenderMessageBL();
            SenderMessageDO vSenderMessageDO = null;
            MessengerSyntaxDO vMessengerSyntaxDO = null;
            try
            {
                vMessengerSyntaxDO = vMessengerSyntaxBL.GetByMessSyntax_Syntax(cmdcode.Trim(), shortcode.Trim());
                #region  kiểm tra xem cú pháp tin nhắn có đúng hay không
                if (vMessengerSyntaxDO.Syntax == null)
                {
                    return 404;
                }
                #endregion
                int rowCount = 0;
                #region neu sau 18 h
                if (DateTime.Now.Hour >= 18 && DateTime.Now.Second >= 1)
                {

                    DataTable tbl = vResultLottery.GETALL_RESULT_PROVINCE_DATE(DateTime.Now, vMessengerSyntaxDO.ProvinceID, 1, 1, ref rowCount);
                    #region Có kết quả
                    if (tbl.Rows.Count > 0)
                    {
                        #region kiểm tra xem có phải kết quả miền bắc
                        if (vMessengerSyntaxDO.ProvinceID == "TD")
                        {
                            foreach (DataRow row in tbl.Rows)
                            {
                                string body = cmdcode.Replace("XS", string.Empty) + DateTime.Now.ToString("dd/MM") + Environment.NewLine +
                                                   "DB:" + row["SPEC"].ToString() + Environment.NewLine +
                                                   "1:" + row["ONE"].ToString() + Environment.NewLine +
                                                   "2:" + row["TWO"].ToString() + Environment.NewLine +
                                                   "3:" + row["THREE"].ToString() + Environment.NewLine +
                                                   "4:" + row["FOUR"].ToString() + Environment.NewLine +
                                                   "5:" + row["FIVE"].ToString() + Environment.NewLine +
                                                   "6:" + row["SIX"].ToString() + Environment.NewLine +
                                                   "7:" + row["SEVEN"].ToString();

                                using (ContentRouting vContentRouting = new ContentRouting())
                                {
                                    vContentRouting.SendMT(shortcode.Trim(), msisdn, mtseq, "text", string.Empty, body, moseq, 1, 1, 1);
                                }

                            }
                        }
                        #endregion
                        #region không phải kết quả miền bắc
                        else
                        {

                            foreach (DataRow row in tbl.Rows)
                            {
                                string body = cmdcode.Replace("XS", string.Empty) + DateTime.Now.ToString("dd/MM") + Environment.NewLine +
                                                   "DB:" + row["SPEC"].ToString() + Environment.NewLine +
                                                   "1:" + row["ONE"].ToString() + Environment.NewLine +
                                                   "2:" + row["TWO"].ToString() + Environment.NewLine +
                                                   "3:" + row["THREE"].ToString() + Environment.NewLine +
                                                   "4:" + row["FOUR"].ToString() + Environment.NewLine +
                                                   "5:" + row["FIVE"].ToString() + Environment.NewLine +
                                                   "6:" + row["SIX"].ToString() + Environment.NewLine +
                                                   "7:" + row["SEVEN"].ToString() + Environment.NewLine +
                                                   "8" + row["eight"].ToString();

                                using (ContentRouting vContentRouting = new ContentRouting())
                                {
                                    vContentRouting.SendMT(shortcode.Trim(), msisdn, mtseq, "text", string.Empty, body, moseq, 1, 1, 1);
                                }

                            }

                        }
                        #endregion
                        #region kiểm tra đầu số nếu là đầu số 6573 thềm vào trong data
                        if (shortcode.Trim().Equals("6573"))
                        {
                            vSenderMessageDO = new SenderMessageDO();
                            vSenderMessageDO.Phone = msisdn.Trim();
                            vSenderMessageDO.Province_ID = vMessengerSyntaxDO.ProvinceID;
                            vSenderMessageDO.Syntax = cmdcode.Trim();
                            vSenderMessageDO.Switch_board = shortcode.Trim();
                            vSenderMessageDO.Status = 1;
                            vSenderMessageDO.Dayregister = DateTime.Today.AddDays(4);
                            vSenderMessageDO.Moseq = moseq.Trim();
                            vSenderMessageDO.Mtseq = mtseq;
                            vSenderMessageBL.SaveSenderMessage(vSenderMessageDO);

                        }
                        #endregion
                    }
                    #endregion
                    #region không có kết quả thêm thông tin đầu số vào data
                    else
                    {
                        #region nếu là đầu số 6573 thêm 5 ngày vào trong data
                        if (shortcode.Trim().Equals("6573"))
                        {
                            vSenderMessageDO = new SenderMessageDO();
                            vSenderMessageDO.Phone = msisdn.Trim();
                            vSenderMessageDO.Province_ID = vMessengerSyntaxDO.ProvinceID;
                            vSenderMessageDO.Syntax = cmdcode.Trim();
                            vSenderMessageDO.Switch_board = shortcode.Trim();
                            vSenderMessageDO.Status = 1;
                            vSenderMessageDO.Dayregister = DateTime.Today.AddDays(4);
                            vSenderMessageDO.Moseq = moseq.Trim();
                            vSenderMessageDO.Mtseq = mtseq;
                            vSenderMessageBL.SaveSenderMessage(vSenderMessageDO);

                        }
                        #endregion
                        #region nếu là đầu số 6173 thì thêm vào data của ngày hiện tại
                        else if (shortcode.Trim().Equals("6173"))
                        {
                            vSenderMessageDO = new SenderMessageDO();
                            vSenderMessageDO.Phone = msisdn.Trim();
                            vSenderMessageDO.Province_ID = vMessengerSyntaxDO.ProvinceID;
                            vSenderMessageDO.Syntax = cmdcode.Trim();
                            vSenderMessageDO.Switch_board = shortcode.Trim();
                            vSenderMessageDO.Status = 1;
                            vSenderMessageDO.Dayregister = DateTime.Now;
                            vSenderMessageDO.Moseq = moseq.Trim();
                            vSenderMessageDO.Mtseq = mtseq;
                            vSenderMessageBL.SaveSenderMessage(vSenderMessageDO);
                        }
                        #endregion
                        else { return 404; }
                    }
                    #endregion


                    return 202;
                }
                #endregion
                #region truoc 18h
                else
                {

                    DataTable tbl = vResultLottery.GETALL_RESULT_PROVINCE_DATE(DateTime.Today.AddDays(-1), vMessengerSyntaxDO.ProvinceID, 1, 1, ref rowCount);
                    #region có kết quả
                    if (tbl.Rows.Count > 0)
                    {
                        if (vMessengerSyntaxDO.ProvinceID == "TD")
                        {
                            foreach (DataRow row in tbl.Rows)
                            {

                                string body = cmdcode.Replace("XS", string.Empty) + DateTime.Today.AddDays(-1).ToString("dd/MM") + Environment.NewLine +
                                                "DB:" + row["SPEC"].ToString() + Environment.NewLine +
                                                "1:" + row["ONE"].ToString() + Environment.NewLine +
                                                "2:" + row["TWO"].ToString() + Environment.NewLine +
                                                "3:" + row["THREE"].ToString() + Environment.NewLine +
                                                "4:" + row["FOUR"].ToString() + Environment.NewLine +
                                                "5:" + row["FIVE"].ToString() + Environment.NewLine +
                                                "6:" + row["SIX"].ToString() + Environment.NewLine +
                                                "7:" + row["SEVEN"].ToString();

                                using (ContentRouting vContentRouting = new ContentRouting())
                                {
                                   // vContentRouting.SendMT(shortcode, msisdn, mtseq, "text", string.Empty, body, moseq, 1, 1, 1);
                                }
                            }
                        }
                        else
                        {
                            foreach (DataRow row in tbl.Rows)
                            {

                                string body = cmdcode.Replace("XS", string.Empty) + DateTime.Today.AddDays(-1).ToString("dd/MM") + Environment.NewLine +
                                                "DB:" + row["SPEC"].ToString() + Environment.NewLine +
                                                "1:" + row["ONE"].ToString() + Environment.NewLine +
                                                "2:" + row["TWO"].ToString() + Environment.NewLine +
                                                "3:" + row["THREE"].ToString() + Environment.NewLine +
                                                "4:" + row["FOUR"].ToString() + Environment.NewLine +
                                                "5:" + row["FIVE"].ToString() + Environment.NewLine +
                                                "6:" + row["SIX"].ToString() + Environment.NewLine +
                                                "7:" + row["SEVEN"].ToString() + Environment.NewLine +
                                                "8" + row["eight"].ToString();

                                using (ContentRouting vContentRouting = new ContentRouting())
                                {
                                    //vContentRouting.SendMT(shortcode, msisdn, mtseq, "text", string.Empty, body, moseq, 1, 1, 1);
                                }
                            }
                        }

                        if (shortcode.Trim().Equals("6573"))
                        {
                            vSenderMessageDO = new SenderMessageDO();
                            vSenderMessageDO.Phone = msisdn.Trim();
                            vSenderMessageDO.Province_ID = vMessengerSyntaxDO.ProvinceID;
                            vSenderMessageDO.Syntax = cmdcode.Trim();
                            vSenderMessageDO.Switch_board = shortcode.Trim();
                            vSenderMessageDO.Status = 1;
                            vSenderMessageDO.Dayregister = DateTime.Today.AddDays(3);
                            vSenderMessageDO.Moseq = moseq.Trim();
                            vSenderMessageDO.Mtseq = mtseq;
                            vSenderMessageBL.SaveSenderMessage(vSenderMessageDO);
                        }
                        return 202;
                    }
                    #endregion
                    #region chưa có kết quả
                    else
                    {
                        if (shortcode.Trim().Equals("6573"))
                        {
                            vSenderMessageDO = new SenderMessageDO();
                            vSenderMessageDO.Phone = msisdn.Trim();
                            vSenderMessageDO.Province_ID = vMessengerSyntaxDO.ProvinceID;
                            vSenderMessageDO.Syntax = cmdcode.Trim();
                            vSenderMessageDO.Switch_board = shortcode.Trim();
                            vSenderMessageDO.Status = 1;
                            vSenderMessageDO.Dayregister = DateTime.Today.AddDays(4);
                            vSenderMessageBL.SaveSenderMessage(vSenderMessageDO);
                            //return
                        }

                        else { return 404; }

                    }
                    #endregion

                }
                #endregion
               
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return 202;
        }
    }
}

//
//Xử lý dữ liệu
//Gọi hàm SendMT để bắn MT.
//

//try
//{
//    using (ContentRouting vContentRouting = new ContentRouting())
//    {
//        //vContentRouting.SendMT("đầu số", "số điện thoại", "mã tin MO", "loại tin nhắn (text)", string.Empty, "Nội dung tin nhắn", "Mã tin MO", "Có tính phí hay không? 1||0", "Tổng số MT trả về (160 ký tự là 1 MT)", "Thứ tự của tin MT");
//    }
//}
//catch (Exception ex)
//{
//    throw ex;
//}
